package com.yz.android_baozou.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.yz.android_baozou.R;
import com.yz.android_baozou.fragment.ColumnFragment;
import com.yz.android_baozou.fragment.HomeFragment;
import com.yz.android_baozou.fragment.RankFragment;
import com.yz.android_baozou.fragment.SearchFragment;
import com.yz.android_baozou.helper.FragmentHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    @Bind(R.id.imageView_showFirstPage_main)
    //声明欢迎页的图片
            ImageView imageViewShowFirstPageMain;
    //声明碎片集合
    private List<Fragment> mList = new ArrayList<>();
    //声明碎片管理器
    private FragmentManager manager = null;
    //声明Toolbar
    private Toolbar toolbar;
    //声明上下文
    private Context mContext = this;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    imageViewShowFirstPageMain.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    };

    //重写OnCreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        //初始化抽屉
        initDrawer();
        //初始化碎片
        initFragment();
        //初始化组件
        initView();

    }

    private void initDrawer() {
        toolbar = (Toolbar) findViewById(R.id.toolbar_main);

        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView_main);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void initFragment() {
        //依次添加4个主页面碎片
        mList.add(new HomeFragment());
        mList.add(new RankFragment());
        mList.add(new ColumnFragment());
        mList.add(new SearchFragment());
    }

    private void initView() {
        manager = getSupportFragmentManager();
        //1平移动画
//        int width = getResources().getDisplayMetrics().widthPixels;
//        Animator animatorX = ObjectAnimator.ofFloat(imageViewShowFirstPageMain, "translationX", 0, -width);
//        ((ObjectAnimator) animatorX).setDuration(4000);
//        ((ObjectAnimator) animatorX).setRepeatMode(ValueAnimator.RESTART);
//        animatorX.start();
        //欢迎页 同时加载网络数据

        // 2通过子线程 控制 图片消失
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        //设置图片不可见
//                        imageViewShowFirstPageMain.setVisibility(View.INVISIBLE);
//                    }
//                });
//            }
//        }).start();
        //设置首页在最初页面显示 handler
        handler.sendEmptyMessageDelayed(1,2000);
        FragmentHelper.switchFragment(manager, mList, 0, R.id.framelayout_container_main, 0, 0);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        Inflate the menu; this adds items to the action bar if it is present.
//        menu.clear();
//        getMenuInflater().inflate(R.menu.main, menu);
//        return true;
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        //根据点击事件依次跳转
        if (id == R.id.action_home) {
            toolbar.setTitle("暴走日报");
            FragmentHelper.switchFragment(manager, mList, 0, R.id.framelayout_container_main, 0, 0);
        } else if (id == R.id.action_rank) {
            toolbar.setTitle("排行榜");
            FragmentHelper.switchFragment(manager, mList, 1, R.id.framelayout_container_main, 0, 0);
        } else if (id == R.id.action_column) {
            toolbar.setTitle("频道");
            FragmentHelper.switchFragment(manager, mList, 2, R.id.framelayout_container_main, 0, 0);
        } else if (id == R.id.action_search) {
            toolbar.setTitle("搜索");
            FragmentHelper.switchFragment(manager, mList, 3, R.id.framelayout_container_main, 0, 0);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}
